<template>
  <Layout>
    <NavBar :isShowLeftIcon="true" iconName="left" navTitle="编辑标签"/>
    <div class="form-wrapper">
      <FromItem :value="tag.name"
                @update:value="updateTag"
                field-name="标签名"
                placeholder="请输入标签名"/>
    </div>
    <div class="button-wrapper">
      <Button @click="remove">删除标签</Button>
    </div>
  </Layout>
</template>

<script lang="ts">
import Vue from 'vue';
import {Component} from 'vue-property-decorator';
import FromItem from '@/components/Common/FormItem.vue';
import Button from '@/components/Common/Button.vue';

@Component({
  components: {Button, FromItem}
})
export default class EditLabel extends Vue {

  get tag() {
    return this.$store.state.currentTag;
  }

  created() {
    this.$store.dispatch('getTagList');
    this.$store.commit('findTag', this.$route.params.id);
    if (!this.tag) {
      this.$router.replace('/404');
    }
  }

  updateTag(name: string) {
    if (this.tag) {
      this.$store.dispatch('saveTags', {id: this.tag.id, name, isAdd: false});
    }
  }

  remove() {
    if (this.tag) {
      this.$store.dispatch('delTag', this.tag.id);
    }
  }
}
</script>

<style lang="scss" scoped>

.form-wrapper {
  background-color: white;
}

.button-wrapper {
  text-align: center;
  padding: 44-16px;
}
</style>
